package com.zwz.utils;

// JavaParser 核心包

import com.github.javaparser.JavaParser;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;

// AST 节点类
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;

// 类型和代码生成工具
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.printer.PrettyPrinter;
// 新版本 JavaParser 5.x 的导入
import com.github.javaparser.printer.DefaultPrettyPrinter;
import com.github.javaparser.printer.configuration.PrettyPrinterConfiguration;
// 文件操作
import java.io.FileInputStream;
import java.io.IOException;


/**
 * 仿照get/sett 方法类
 */

public class GetterSetterGenerator {
    public static void main(String[] args) {
        new GetterSetterGenerator().method();
    }

    public void method() {
        try {
            // 1. 读取 Java 文件并解析为 AST
            FileInputStream in = new FileInputStream("src/main/java/com/zwz/resp/annualReports/WebLink.java");
            JavaParser demo = new JavaParser();
            CompilationUnit cu = StaticJavaParser.parse(in);


            // 2. 查找目标类
            cu.getClassByName("WebLink").ifPresent(clazz -> {
                // 3. 遍历所有字段
                clazz.getFields().forEach(field -> {
                    // 为每个字段生成 getter 和 setter
                    generateGetter(clazz, field);
                    generateSetter(clazz, field);
                });
            });

            // 4. 输出生成的代码到控制台（或写回文件）
            PrettyPrinter printer = new PrettyPrinter(
                    new PrettyPrinterConfiguration()
                            .setOrderImports(true)
                            .setIndentSize(4)
            );
            System.out.println(printer.print(cu));

            // 可选：将修改后的 AST 写回文件
            // Path outputPath = Paths.get("src/main/java/your/package/YourClass.java");
            // Files.writeString(outputPath, cu.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void generateGetter(ClassOrInterfaceDeclaration clazz, FieldDeclaration field) {
        // 提取字段名和类型
        String fieldName = field.getVariable(0).getNameAsString();
        Type fieldType = field.getCommonType();

        // 生成 getter 方法名（例如：getName）
        String methodName = "get" + capitalize(fieldName);

        // 创建方法声明
        MethodDeclaration getter = new MethodDeclaration();
        getter.addModifier(Modifier.Keyword.PUBLIC);
        getter.setType(fieldType);
        getter.setName(methodName);

        // 添加方法体：return this.fieldName;
        getter.setBody(
                StaticJavaParser.parseBlock("{ return this." + fieldName + "; }")
        );

        // 将方法添加到类中
        clazz.addMember(getter);
    }

    private static void generateSetter(ClassOrInterfaceDeclaration clazz, FieldDeclaration field) {
        String fieldName = field.getVariable(0).getNameAsString();
        Type fieldType = field.getCommonType();

        String methodName = "set" + capitalize(fieldName);

        MethodDeclaration setter = new MethodDeclaration();
        setter.addModifier(Modifier.Keyword.PUBLIC);
        setter.setType(void.class); // setter 返回类型为 void
        setter.setName(methodName);

        // 添加参数：参数类型和字段一致
        Parameter param = new Parameter(fieldType, fieldName);
        setter.addParameter(param);

        // 添加方法体：this.fieldName = fieldName;
        setter.setBody(
                StaticJavaParser.parseBlock("{ this." + fieldName + " = " + fieldName + "; }")
        );

        clazz.addMember(setter);
    }

    /**
     * 工具方法：首字母大写
     */
    private static String capitalize(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }
}